
local japi = require 'jass.japi'
local ui = class.panel.create('',1,1,1,1)

local buffs = {
    '双击复活',
    '技能蓄力',
    '击飞',
    '晕眩',
    '蓄力中',
    '被锁定',
}
function create_life1(w,h)
    local panel = class.panel:builder
    {
        parent = ui,
        x = -800,
        y = -200,
        w = w,
        h = h,
        show_flag = false,
        world_auto_remove = false,
        --parent = class.ui_base.portrait,
        normal_image = [[core\hpbar\backdrop-hero.tga]],
        world_anchor = 'top',
        updata_buff = function(self,unit)
            for _,name in ipairs(buffs) do
                local buff = unit:find_buff(name)
                if buff then
                    local title = buff:get_title()
                    if self.title.text~=title then
                        self.title:set_text(title)
                    end
                    if buff.time>0 then
                        self.state:set_width(self.max_w*buff:get_remaining()/buff.time)
                    else
                        self.state:set_width(self.max_w)
                    end
                    self.state:show()
                    return
                end
            end
            local title = unit.name
            if unit.owner:is_player() then
                title = unit.owner:get_world_name()
            end
            if self.title.text~=title then
                self.title:set_text(title)
            end
            self.state:hide()
        end,
        updata_shield = function(self,unit)
            local hpmax = unit:get '生命上限'
            local shield1 = unit:get '护盾'
            local life = unit:get '生命'
            if shield1>0 then
                local race = shield1/hpmax
                if shield1+life>hpmax then
                    race = math.min(race,1)
                    self.shield:set_position(self.max_w*(1-race),0)
                else
                    self.shield:set_position(self.life.w,0)
                end
                self.shield:set_width(self.max_w*race)
                self.shield:show()
            else
                self.shield:hide()
            end
        end,
        updata_cost = function(self,unit)
            local cost = unit:get '魔法'
            local mpmax = unit:get '魔法上限'
            local image = unit:get_data('使用能量')
            if image then
                cost = unit:get '能量'
                mpmax = unit:get '能量上限'
                self.cost:set_normal_image(image)
            else
                self.cost:set_normal_image([[core\hpbar\蓝条.tga]])
            end
            if mpmax>0 and cost>0 then
                self.cost:set_width(self.max_w*cost/mpmax)
                self.cost:show()
            else
                self.cost:hide()
            end
        end,
        update = function(self)
            local player = ac.player.self
            local unit = self.world_widget
            local life = unit:get '生命'
            local hpmax = unit:get '生命上限'
            if unit.level_class then
                self.rank:set_text(unit.level_class)
            else
                self.rank:set_text(unit.level)
            end
            self.life:set_width(self.max_w*life/hpmax + 0.1)
            if self.updata_buff then
                self:updata_buff(unit)
            end
            if self.updata_cost then
                self:updata_cost(unit)
            end
            if self.updata_shield then
                self:updata_shield(unit)
            end
            local cost_path = unit:get_data('能量贴图')
            if player==unit.owner then
                self.life:set_normal_image([[core\hpbar\绿条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\蓝条.tga]])
            elseif player:is_ally(unit) then
                self.life:set_normal_image([[core\hpbar\蓝条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\蓝条.tga]])
            elseif player:is_enemy(unit) and unit.owner~=ac.player[16] then
                self.life:set_normal_image([[core\hpbar\红条.tga]])
                self.cost:set_normal_image(cost_path or [[core\hpbar\红条.tga]])
            else
                self.life:set_normal_image([[core\hpbar\黄条.tga]])
            end
        end,
    }

    local rank = panel:add_panel([[core\hpbar\backdrop-level.tga]],-25,-1,25,27)

    panel.rank = rank:add_text('21',-1,2,25,25,8,5)
    panel.title = panel:add_text('筑基中期 手残玩家',-2,-15,w,10,8,5)
    panel.state = panel:add_panel([[core\hpbar\黄条.tga]],1,-3,w-5,3)
    panel.life = panel:add_panel([[core\hpbar\绿条.tga]],1,3,w-5,11)
    panel.cost = panel:add_panel([[core\hpbar\蓝条.tga]],1,17,w-5,3)

    panel.shield = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.shield:set_alpha(0.9)
    panel.shield:hide()

    panel.life:add_panel([[core\hpbar\Line.tga]])
    panel.max_w = panel.life.w
    panel:set_level(50)
    panel:hide()

    return panel
end

local w,h = 130,25
--重复利用组
local group1 = {}
local remove1 = {}

function unit_add_life(unit)
    local panel = table.remove(remove1,1)
    if not panel then
        panel = create_life1(w,h)
    end

    panel:bind_world(unit)
    japi.SetUnitPressUIVisible(unit.handle,false)
    group1[unit.handle] = panel

    unit:event '单位-即将移除'(function()
        group1[unit.handle] = nil
        table.insert(remove1,panel)
    end)
    return panel
end



local buffs = {
    '双击复活',
    '技能蓄力',
    '击飞',
    '晕眩',
    '蓄力中',
    '被锁定',
}
function create_life2(w,h)
    local panel = class.panel:builder
    {
        x = -800,
        y = -200,
        w = w,
        h = h,
        scale = 1,
        is_show = false,
        world_auto_remove = false,
        parent = ac.frame.portrait,
        world_anchor = 'bottom',
        normal_image = [[core\hpbar\backdrop-unit.tga]],
        buffer_rate = 1,
        updata_buff = function(self,unit)
            for _,name in ipairs(buffs) do
                local buff = unit:find_buff(name)
                if buff then
                    local title = buff:get_title()
                    if self.title.text~=title then
                        self.title:set_text(title)
                    end
                    if buff.time>0 then
                        self.state:set_width(self.max_w*buff:get_remaining()/buff.time)
                    else
                        self.state:set_width(self.max_w)
                    end
                    self.state:show()
                    return
                end
            end
            -- local title = unit.name
            -- if unit.owner:is_player() then
            --     title = unit.owner:get_world_name()
            -- end
            -- if self.title.text~=title then
            --     self.title:set_text(title)
            -- end
            self.state:hide()
        end,
        updata_shield = function(self,unit)
            local hpmax = unit:get '生命上限'
            local shield = unit:get '护盾'
            local life = unit:get '生命'
            local rate = shield>0 and shield/hpmax
            if shield>0 then
                if unit.shield_update then
                    rate = unit:shield_update()
                    --self.shield:set_position(0,0)
                else
                    -- if shield+life>hpmax then
                    --     rate = math.min(rate,1)
                    --     self.shield:set_position(self.max_w*(1-rate) * self.scale,0)
                    -- else
                    --     self.shield:set_position(self.life.w * self.scale,0)
                    -- end
                end
                self.shield:set_width(self.max_w*rate * self.scale)
                self.shield:show()
            else
                self.shield:hide()
            end
        end,
        update = function(self)
            local player = ac.player.self
            local unit = self.world_widget
            local life = unit:get '生命'
            local hpmax = unit:get '生命上限'
            local rate = life/hpmax
            
            local load1 = math.max(math.abs(rate - self.buffer_rate)*0.05,0.01)
            if rate>self.buffer_rate then
                self.buffer_rate = math.min(self.buffer_rate + load1,rate)
            elseif rate<self.buffer_rate then
                self.buffer_rate = math.max(self.buffer_rate - load1,rate)
            end
            if unit.hide_buffer==nil and self.buffer_rate~=rate then
                local start = math.min(rate,self.buffer_rate)
                local size = math.abs(rate - self.buffer_rate)
                self.buffer:set_position(self.max_w* start * self.scale,self.buffer.y)
                self.buffer:set_width(self.max_w* size * self.scale+0.01)
                self.buffer:show()
            else
                self.buffer:hide()
            end

            self.life:set_width(self.max_w* rate * self.scale)
            -- if self.updata_shield then
            --     self:updata_shield(unit)
            -- end
            -- if self.updata_buff then
            --     self:updata_buff(unit)
            -- end
            local image = unit:get_data('血条贴图')
            if image==nil then
                if player==unit.owner then
                    self.life:set_normal_image([[core\hpbar\绿条.tga]])
                elseif player:is_enemy(unit.owner) then
                    self.life:set_normal_image([[core\hpbar\红条.tga]])
                elseif player:is_ally(unit.owner) then
                    self.life:set_normal_image([[core\hpbar\蓝条.tga]])
                else
                    self.life:set_normal_image([[core\hpbar\黄条.tga]])
                end
            else
                self.life:set_normal_image(image)
            end
            local title = unit.ui_title or unit:get_name()
            if self.title.text~=title then
                self.title:set_text(title)
            end
        end,
    }

    local x1 = panel.w*0.02
    local y1 = panel.h*0.15
    local w1 = panel.w*0.97
    local h1 = panel.h*0.7
    local h2 = panel.h*0.3
    panel.title = class.text:builder {
        parent = panel,
        x = -100,
        y = -62,
        w = w1+200,
        h = 60,
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow_color = 0xff000000,
        outline = true,
        show_flag = true,
		font_size = 10,
        align = 2,
    }
    --panel.state = panel:add_panel([[core\hpbar\黄条.tga]],x1,-3,w1,3)
    panel.life = panel:add_panel([[core\hpbar\ui_icon_05.tga]],x1,y1,w1,h1)
    -- panel.shield = panel:add_panel([[core\hpbar\ShieldBar.tga]],x1,y1,w1,h1)
    -- panel.shield:set_alpha(0.5)
    -- panel.shield:hide()
    panel.x1 = x1

    panel.buffer = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.buffer:set_alpha(0.5)
    panel.max_w = panel.life.w
    panel.life_h = panel.life.h
    panel:hide()

    return panel
end

local remove2 = {}
local group2 = {}
local w,h = 100,10
function unit_add_life2(unit,scale)
    if group2[unit.handle] then
        local panel = group2[unit.handle]
        panel.buffer_rate = 1
        if scale then
            panel.scale = scale
            panel:set_relative_size(scale)
        else
            local scale = unit:get_select_scale()
            panel.scale = scale
            panel.title:set_size(1)
            panel:set_relative_w(scale)
        end
        return panel
    end
    local panel = table.remove(remove2,1)
    if not panel then
        panel = create_life2(w,h)
    end

    panel.buffer_rate = 1
    if scale then
        panel.scale = scale
        panel:set_relative_size(scale)
    else
        local scale = unit:get_select_scale()
        panel.scale = scale
        panel.title:set_size(1)
        panel:set_relative_w(scale)
    end

    local title = unit.ui_title or unit:get_name()
    panel.title:set_text(title)
    
    panel:bind_world(unit)
    japi.SetUnitPressUIVisible(unit.handle,false)
    group2[unit.handle] = panel

    unit:event '单位-即将移除'(function()
        group2[unit.handle] = nil
        table.insert(remove2,panel)
    end)

    return panel
end






ac.hardware:on "界面绘制" (function ()
    for handle,panel in pairs(group1) do
        if panel.show_flag then
            panel:update()
        end
    end

    for handle,panel in pairs(group2) do
        if panel.show_flag then
            panel:update()
        end
    end
end)
